// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    private lastPosition:cc.Vec2;
    private boxCollider:cc.BoxCollider;
    private flag:boolean = false;
    private wall:cc.BoxCollider;

    onLoad(){
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        //manager.enabledDebugDraw = true;

        this.wall = this.node.parent.getChildByName("wall").getComponent(cc.BoxCollider);

        cc.game.setFrameRate(60);
        this.boxCollider = this.getComponent(cc.BoxCollider);
        this.node.on(cc.Node.EventType.TOUCH_END,()=>{
            this.flag=!this.flag
        },this)
    }
    start () {

    }
    update (dt) {
        this.lastPosition = this.node.position;

        this.node.y-=10;

        //cc.log("update",this.boxCollider.world.aabb);
    }
    lateUpdate(){
        //cc.log(this.boxCollider.world.preAabb,this.boxCollider.world.aabb)
        //cc.Intersection.rectRect(this.boxCollider.world.preAabb)

        if(cc.Intersection.rectRect(this.boxCollider.world.aabb,this.wall.world.aabb)){
            this.node.position = this.lastPosition;
            cc.log(this.boxCollider.world.aabb,this.boxCollider.world.preAabb)
            return;
        }


        //this.node.position = this.lastPosition;
        // if(this.flag){
        //     cc.log(this.lastPosition,this.node.position);
        //     cc.game.pause();    
        // }
        //cc.log("lateUpdateBefore",this.boxCollider.world.aabb);
        //cc.log("lateUpdate");
    }
    onCollisionEnter(){
        cc.log("碰撞了");        
    }
}
